#!/bin/bash
set -ue


cur_dir=`pwd`

source_list="docker-lxcfs-toolkit docker-toolkit docker-tools elara"
current_dir="$cur_dir/../../../../../code/current"
base_dir="$cur_dir/../../../../../code/base"
split_dir="split_source"
repo_dir_list="euleros/self_src/kernelspace  euleros/self_src/misc  euleros/self_src/userspace"

function create_current_src()
{
    rm -rf $current_dir/$split_dir
    rm -rf $current_dir/next_source/$split_dir
    mkdir -p $current_dir/$split_dir
    ##for govet
    for module in `echo "$source_list" | xargs`
    do
        cd $current_dir/next_source/euleros/self_src/userspace/${module}
        for dir in `find . -name vendor -type d`
        do
            if [ -d $dir ];then
                rm -rf $dir
            fi
        done
        cd -
        cp -r $current_dir/next_source/euleros/self_src/userspace/${module} $current_dir/$split_dir
        rm -rf $current_dir/$split_dir/${module}/.git
    done
    pushd $current_dir/$split_dir
    rm -rf ./docker-toolkit/docker-toolkit/deps
    rm -f ./elara/elara-*/manager/nodeHanlder.go
    rm -f ./elara/elara-*/volume/volume.go
    popd
    mv $current_dir/$split_dir $current_dir/next_source
}

function create_base_by_time()
{
    if [ ! -d $base_dir/next_source ]; then
        rm -rf $base_dir
        mkdir -p $base_dir

        sleep 10

        cp -a $current_dir/next_source $base_dir/ 
        for line in `find $base_dir/next_source/euleros -name .git`
        do
            pushd $line/..
            git clean -df 
            git checkout $(git log --before "2018-07-01 20:00:00" -1 --format="%H")
            popd
        done
    fi
}

function delete_libstorage()
{
    cd $current_dir
    rm -rf ./next_source/euleros/self_src/userspace/ublock
    rm -rf ./next_source/euleros/self_src/userspace/libstorage-uio
    rm -rf ./next_source/euleros/self_src/kernelspace/ipvs-fnat
}


#delete_libstorage
create_base_by_time
create_current_src
